package com.jshoperxms.service.impl;

import com.jshoperxms.controller.mall.backstage.vo.sysrole.SysRoleVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.dao.SysModuleActionTDao;
import com.jshoperxms.entity.SysModuleActionT;
import com.jshoperxms.service.SysModuleActionTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/6/8.
 * Des:
 */
@Service("sysModuleActionTService")
@Scope("prototype")
public class SysModuleActionTServiceImpl extends  BaseTServiceImpl<SysModuleActionT> implements SysModuleActionTService {

    @Resource
    private Serial serial;
    @Resource
    private SysModuleActionTDao sysModuleActionTDao;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void saveOrupdateSysModuleActionTransactional(SysModuleActionT rq) {
        List<SysRoleVo>sysRoleVos= GsonJson.parseJsonToData(rq.getActionurl(),SysRoleVo.class);
        if(!sysRoleVos.isEmpty()){
            for(SysRoleVo s:sysRoleVos){
                if(StringUtils.equals(StaticKey.ZERO,s.getId())){
                    //保存
                    SysModuleActionT bean=new SysModuleActionT();
                    bean.setId(this.serial.Serialid(SerialKeys.SYSMODULEACTIONT));
                    bean.setActionurl(s.getActionurl());
                    bean.setCreatetime(DateTools.getSystemTimeBi());
                    bean.setCreatorid(BaseTools.getAdminCreateId());
                    bean.setSort(1);
                    bean.setResourcetype(s.getResourcetype());
                    bean.setPermission(s.getPermission());
                    bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                    bean.setSysmoduleactionname(s.getSysmoduleactionname());
                    bean.setSysmoduleid(rq.getSysmoduleid());
                    bean.setSysmodulename(rq.getSysmodulename());
                    bean.setType(s.getType());
                    bean.setUpdatetime(DateTools.getSystemTimeBi());
                    bean.setVersiont(1);
                    this.sysModuleActionTDao.save(bean);
                }else{
                    SysModuleActionT bean=this.sysModuleActionTDao.findByPK(SysModuleActionT.class,s.getId());
                    if(bean!=null){
                        //更新
                        bean.setActionurl(s.getActionurl());
                        bean.setSysmoduleactionname(s.getSysmoduleactionname());
                        bean.setSysmoduleid(rq.getSysmoduleid());
                        bean.setSysmodulename(rq.getSysmodulename());
                        bean.setType(s.getType());
                        bean.setResourcetype(s.getResourcetype());
                        bean.setPermission(s.getPermission());
                        bean.setUpdatetime(DateTools.getSystemTimeBi());
                        bean.setVersiont(bean.getVersiont()+1);
                        this.sysModuleActionTDao.update(bean);
                    }
                }
            }

        }
    }
}
